package cn.kkcxy.blog.system.factory.wechat;

import cn.kkcxy.blog.core.enums.WeChatMessageTypeEnum;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class WechatMessageFactory implements InitializingBean {

    @Resource
    private List<WechatMessageHandler> wechatMessageHandlerList;

    private final Map<WeChatMessageTypeEnum, WechatMessageHandler> handlerMap = new HashMap<>();

    public WechatMessageHandler getHandlerByMessageType(String messageType) {
        WeChatMessageTypeEnum typeEnum = WeChatMessageTypeEnum.getByMsgType(messageType);
        return handlerMap.get(typeEnum);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        for (WechatMessageHandler wechatMessageHandler : wechatMessageHandlerList) {
            handlerMap.put(wechatMessageHandler.getMessageType(), wechatMessageHandler);
        }
    }
}
